import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import Forbidden from "@/views/403";
import NotFound from "@/views/404";
import findLast from "lodash/findLast";
import { check, isLogin } from "@/utils/auth";

Vue.use(VueRouter);
// 左侧导航:有meta显示;有hideInMenu隐藏;有hideChildInMenu,隐藏子节点;
// 权限部分：  meta: { authority: ["admin"], icon: "key", title: "系统管理" }  authority是一个数组，可以写多个
const routes = [
  {
    path: "/user/login",
    name: "login",
    hideInMenu: true,
    component: () => import(/* webpackChunkName: "user" */ "../views/Login.vue")
  },
  {
    path: "/",
    redirect: "/dashboard",
    meta: { authority: ["admin", "user"] },
    component: () =>
      import(/* webpackChunkName: "layouts" */ "@/views/Layout.vue"),
    children: [
      {
        path: "/dashboard",
        name: "dashboard",
        redirect: "/dashboard/analysis",
        meta: { icon: "dashboard", title: "仪表盘" },
        component: { render: h => h("router-view") },
        children: [
          {
            path: "/dashboard/analysis",
            name: "analysis",
            meta: { title: "分析页" },
            component: () =>
              import(
                /* webpackChunkName: "dashboard" */ "@/views/dashboard/Analysis"
              )
          }
        ]
      },
      {
        path: "/sys",
        name: "sys",
        meta: { authority: ["admin"], icon: "key", title: "系统管理" },
        component: { render: h => h("router-view") },
        children: [
          {
            path: "/sys/resource",
            name: "resource",
            meta: { title: "资源管理" },
            component: () =>
              import(/* webpackChunkName: "sys" */ "@/views/sys/Resource.vue")
          },
          {
            path: "/sys/role",
            name: "role",
            meta: { title: "角色管理" },
            component: () =>
              import(/* webpackChunkName: "sys" */ "@/views/sys/Role.vue")
          },
          {
            path: "/sys/user",
            name: "user",
            meta: { title: "用户管理" },
            component: () =>
              import(/* webpackChunkName: "sys" */ "@/views/sys/User.vue")
          }
        ]
      },
      {
        path: "/403",
        name: "403",
        hideInMenu: true,
        component: Forbidden
      },
      {
        path: "*",
        hideInMenu: true,
        component: NotFound
      }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }
  if (isLogin()) {
    const record = findLast(to.matched, record => record.meta.authority);
    if (record && !check(record.meta.authority)) {
      //没有权限
      next({ path: "/403" });
    } else {
      //有权限,放行,去目标
      next();
    }
  } else {
    //未登录,请求的就是登录页,去目标
    if (to.path === "/user/login") {
      next();
    } else {
      //未登录,请求的非登录页,先登录
      next({ path: "/user/login" });
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
